package helper

import (
	"encoding/json"
	"errors"
	"net/url"
	"sort"
	"strings"
)

func MergeStringMap(x, y map[string]string) map[string]string {

	n := make(map[string]string)
	for i, v := range x {
		for j, w := range y {
			if i == j {
				n[i] = w

			} else {
				if _, ok := n[i]; !ok {
					n[i] = v
				}
				if _, ok := n[j]; !ok {
					n[j] = w
				}
			}
		}
	}

	return n

}

/**
 * 转换为map string数组
 * 2021/1/5
 * gz
 */
func ToMapStringArray(x interface{}) ([]map[string]string, error) {
	list, ok := x.([]interface{})
	if !ok {
		return []map[string]string{}, errors.New("type error")
	}

	if len(list) < 1 {
		return []map[string]string{}, nil
	}
	var ret []map[string]string = make([]map[string]string, len(list))
	var tmp map[string]interface{}
	var map_tmp map[string]string
	var err error

	for key, item := range list {
		if tmp, ok = item.(map[string]interface{}); ok {
			map_tmp = make(map[string]string)
			for k, v := range tmp {
				map_tmp[k] = ToStr(v)
			}
			ret[key] = map_tmp
		} else {
			err = errors.New("data type error")
			break
		}
	}

	return ret, err
}

/**
 * 将map[string]string数组转成map[string]interface数组
 */
func MapStringToInterfaceArr(src []map[string]string) []map[string]interface{} {
	var dest []map[string]interface{}

	for i := range src {
		m := src[i]
		n := make(map[string]interface{})
		for k, v := range m {
			n[k] = interface{}(v)
		}
		dest = append(dest, n)
	}

	return dest
}

/**
 * 将map[string]string转成map[string]interface
 */
func MapStringToInterface(src map[string]string) map[string]interface{} {
	target := make(map[string]interface{})
	for k, v := range src {
		target[k] = interface{}(v)
	}

	return target
}

//将interface转成map[string]interface
func InterfaceToMapInterface(data interface{}) (map[string]interface{}, error) {
	dataJson, err := json.Marshal(data)

	if err != nil {
		return nil, err
	}

	var returnData map[string]interface{}
	err = json.Unmarshal(dataJson, &returnData)

	return returnData, nil
}

//将interface转成map[string]string
func InterfaceToMapString(data interface{}) (map[string]string, error) {
	dataJson, err := json.Marshal(data)

	if err != nil {
		return nil, err
	}

	var returnData map[string]string
	err = json.Unmarshal(dataJson, &returnData)

	return returnData, nil
}

//将interface转成[]map[string]interface
func InterfaceToMapInterfaceArr(data interface{}) ([]map[string]interface{}, error) {
	dataJson, err := json.Marshal(data)

	if err != nil {
		return nil, err
	}

	var returnData []map[string]interface{}
	err = json.Unmarshal(dataJson, &returnData)

	return returnData, nil
}

//将interface转成[]map[string]string
func InterfaceToMapStringArr(data interface{}) ([]map[string]string, error) {
	dataJson, err := json.Marshal(data)

	if err != nil {
		return nil, err
	}

	var returnData []map[string]string
	err = json.Unmarshal(dataJson, &returnData)

	return returnData, nil
}

/**
 * 对map进行字典序排序
 * @param data map[string]string
 * @2020/12/28
 * @linsen
 */
func HttpBuildQuery(data map[string]string) string {

	keySlice := []string{}

	for k, _ := range data {
		keySlice = append(keySlice, k)
	}
	sort.Strings(keySlice) //排序字符串

	var query string

	for _, value := range keySlice {
		if value != "" && data[value] != "" {
			query += StringJoin(value, "=", data[value], "&")
		}
	}

	return strings.Trim(query, "&")
}

/**
 * 对map字典按照顺序进行拼接，并且把每个value进行类urlencode
 * @param data map[string]string
 * @2021/10/14
 * @wyy
 */
func HttpBuildStringQuery(data map[string]string) string {

	keySlice := []string{}

	for k, _ := range data {
		keySlice = append(keySlice, k)
	}
	// sort.Strings(keySlice) //排序字符串

	var query string

	for _, value := range keySlice {
		if value != "" && data[value] != "" {
			query += StringJoin(value, "=", url.QueryEscape(data[value]), "&")
		}
	}

	return strings.Trim(query, "&")
}
